package cn.appsys.controller.front;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.appsys.constant.Constant;
import cn.appsys.pojo.DevUser;
import cn.appsys.service.front.user.FrontUserService;

/**
 * 前台登入Controller控制器
 * @author administrader
 *
 */
@Controller
@RequestMapping("/dev")
public class DeveLoginController {

	
	@Resource
	private FrontUserService frontUserService; //创建开发者用户逻辑访问类
	
	
	/**
	 * 跳转到前台登入页面
	 * @return
	 */
	@RequestMapping(value = "/Login")
	public String doLogin(){
		return "devlogin";
	}
	
	/**
	 * 处理登入者信息是否正确
	 * @param devCode  页面获取到的用户名
	 * @param devPassword 页面获取到的密码
	 * @param session 作用域
	 * @param model 视图
	 * @return
	 */
	@RequestMapping(value = "/toLogin")
	public String toLogin(@RequestParam(value="devCode",required=false)String devCode,
			@RequestParam(value="devPassword",required=false)String devPassword,HttpSession session,
			Model model){
		DevUser devUser = frontUserService.checkDevUserService(devCode); //通过用户名称查询用户信息
		if (devUser == null) {
			model.addAttribute("error", "用户名不存在,请确认后重新输入!");
			return "devlogin";
		}else if (!devUser.getDevPassword().equals(devPassword)) {
			model.addAttribute("error", "您输入的密码不正确,请确认后在输入");
			return "devlogin";
		}else {
			session.setAttribute(Constant.DEV_USER_SESSION, devUser); //将用户信息保存到Session作用域
			return "developer/main";
		}
	}
	
	
	/**
	 * 注销
	 * @param session
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpSession session){
		session.removeAttribute(Constant.DEV_USER_SESSION);
		return "devlogin";
	}
}
